package com.xxx.store.controller;

import com.xxx.store.utils.Result;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * TODO
 *
 * @ClassName
 * @Author FGH
 * @Date 2023/4/4 12:02 下午
 **/
@Controller
public class UploadController {


    @PostMapping("/upload")
    @ResponseBody
    public Result uploadCover(@RequestParam("file") MultipartFile file) throws IOException {
        String name = System.currentTimeMillis() + "";
        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String ext = originalFilename.substring(index);
        //构造路径  得到的是当前磁盘的目录 比如 D:/abc 最后没有斜杆的
        //System.getProperty("user.dir") == 当前项目根目录
        String absolutePath = System.getProperty("user.dir");
        // 文件不存在创建新的文件
        File directory = new File(absolutePath + "/upload/");
        if (!directory.exists()) {
            //创建文件夹
            directory.mkdir();
        }
        String newFilePath = absolutePath + "/upload/" + name + ext;
        File newFile = new File(newFilePath);
        file.transferTo(newFile);

        return Result.ok("上传成功", "http://localhost:3000/upload/" + name + ext);
    }

    // 下载文件
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    ResponseEntity<InputStreamResource> downloadFile( String name,String url)
            throws IOException {
        String virtualName = url.substring(url.lastIndexOf("/") + 1);
        // 获取 文件的后缀
        String ext = virtualName.substring(virtualName.lastIndexOf('.'));

        String targetFilename = name + ext;
        targetFilename =  URLEncoder.encode(targetFilename, "UTF-8");

        String absolutePath = System.getProperty("user.dir");

        // 得到 target webapp下的目录
//        String webappPath = new File(absolutePath);
        String downloadPath = absolutePath + "/upload/";
        String realFilePath = downloadPath + virtualName;


        FileSystemResource file = new FileSystemResource(realFilePath);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", targetFilename));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }

}
